package com.waterfairy.systemapp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.ImageView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * @user water_fairy
 * @mail 995637517@qq.com
 * @date 2023/10/17 22:12
 * @info
 */
public class WebUtils {
    private static final String TAG = "WebUtils";
    //获取登录二维码
    public static String CMD_IMG = "javascript:document.getElementsByClassName('web-login-scan-code__content__qrcode-wrapper web-login-scan-code__content__qrcode-tip')[0].firstChild.getAttribute('src');";

    //点击 "添加商品"
    public static String CMD_ADD_COMM = "javascript:document.getElementsByClassName('okee-current-live-btn okee-current-live-btn-size-md okee-current-live-btn-type-primary okee-current-live-btn-shape-angle okee-current-live-can-input-grouped xhb4P')[index].click()";

    /**
     * 获取二维码
     *
     * @param webView
     * @param callback
     */
    public static void getQrImg(WebView webView, ValueCallback<String> callback) {
        webView.evaluateJavascript(WebUtils.CMD_IMG, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {
                Log.i(TAG, "onReceiveValue: " + s);
                if (s != null && !s.equals("null")) {
                    s = s.substring(1, s.length() - 1);
                    if (s.startsWith("data:image/jpeg;base64,")) {
                        callback.onReceiveValue(s.replace("data:image/jpeg;base64,", ""));
                    }
                }
            }
        });
    }

    /**
     * 展示二维码
     *
     * @param imageView
     * @param qr
     * @return
     */
    public static Bitmap showQr(ImageView imageView, String qr) {
        byte[] decodedString = Base64.decode(qr, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imageView.setImageBitmap(decodedByte);
        return decodedByte;
    }

    /**
     * 保存二维码到本地
     *
     * @param bitmap
     * @param completedListener
     */
    public static void saveQr(Bitmap bitmap, MediaScannerConnection.OnScanCompletedListener completedListener) {
        File pictures = Environment.getExternalStoragePublicDirectory("Pictures");
        File bitmapFile = new File(pictures, new Date().getTime() + ".png");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(bitmapFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            MediaScannerConnection.scanFile(SystemApp.getApp(), new String[]{bitmapFile.getAbsolutePath()}, null, completedListener);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 自动化点击 "添加商品"
     *
     * @param webView
     * @param index
     */
    public static void addCommodity(WebView webView, int index) {
        String js = WebUtils.CMD_ADD_COMM.replace("index", index + "");

        webView.evaluateJavascript(js, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {
                if (s != null && !s.equals("null")) {

                }
            }
        });
    }

    /**
     * 打开抖音
     */
    public static void openDouyin() {
        Intent intent = SystemApp.getApp().getPackageManager().getLaunchIntentForPackage("com.ss.android.ugc.aweme");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        SystemApp.getApp().startActivity(intent);
    }
}
